package com.ruoyi.information.service.back.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.information.domain.back.CompanyNeedBack;
import com.ruoyi.information.domain.back.SysUserBack;
import com.ruoyi.information.mapper.back.CompanyNeedBackMapper;
import com.ruoyi.information.mapper.back.RecomBackMapper;
import com.ruoyi.information.mapper.back.SysUserBackMapper;
import com.ruoyi.information.service.back.ICompanyNeedBackService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 企业需求Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-05
 */
@Service
public class CompanyNeedBackServiceImpl implements ICompanyNeedBackService {
    @Autowired
    private CompanyNeedBackMapper companyNeedMapper;
    @Autowired
    private SysUserBackMapper sysUserMapper;

    @Autowired
    private RecomBackMapper recomBackMapper;

    /**
     * 查询企业需求
     *
     * @param id 企业需求主键
     * @return 企业需求
     */
    @Override
    public CompanyNeedBack selectCompanyNeedById(Long id) {
        CompanyNeedBack companyNeedBack = companyNeedMapper.selectCompanyNeedById(id);
        if (companyNeedBack != null) {
            if (companyNeedBack.getState() != null && companyNeedBack.getState() == 0) {
                /*修改为已读未处理*/
                CompanyNeedBack needBack = new CompanyNeedBack();
                needBack.setId(id);
                needBack.setState2(1L);
                companyNeedMapper.updateCompanyNeed(needBack);
            }
        }
        return companyNeedBack;
    }

    /**
     * 查询企业需求列表
     *
     * @param companyNeed 企业需求
     * @return 企业需求
     */
    @Override
    public List<CompanyNeedBack> selectCompanyNeedList(CompanyNeedBack companyNeed) {
        if (companyNeed.getEconomic() != null) {
            SysUserBack sysUser = new SysUserBack();
            BeanUtils.copyProperties(companyNeed, sysUser);
            List<Long> userIds = sysUserMapper.selectSysUserListBy(sysUser);
            if (userIds == null || userIds.size() == 0) {
                return new ArrayList<>();
            }
            companyNeed.setUserIds(userIds);
        }
        List<CompanyNeedBack> list = companyNeedMapper.selectCompanyNeedList(companyNeed);
        for (CompanyNeedBack need : list) {
            need.setUser(sysUserMapper.selectSysUserByUserId(need.getUserId()));
        }
        return list;
    }

    /**
     * 新增企业需求
     *
     * @param companyNeed 企业需求
     * @return 结果
     */
    @Override
    public int insertCompanyNeed(CompanyNeedBack companyNeed) {
        companyNeed.setCreateTime(DateUtils.getNowDate());
        return companyNeedMapper.insertCompanyNeed(companyNeed);
    }

    /**
     * 修改企业需求
     *
     * @param companyNeed 企业需求
     * @return 结果
     */
    @Override
    public int updateCompanyNeed(CompanyNeedBack companyNeed) {
        companyNeed.setUpdateTime(DateUtils.getNowDate());
        return companyNeedMapper.updateCompanyNeed(companyNeed);
    }

    /**
     * 批量删除企业需求
     *
     * @param ids 需要删除的企业需求主键
     * @return 结果
     */
    @Override
    public int deleteCompanyNeedByIds(Long[] ids) {
        recomBackMapper.deleteRecomByIdsAndType(ids, "企业需求");
        return companyNeedMapper.deleteCompanyNeedByIds(ids);
    }

    /**
     * 删除企业需求信息
     *
     * @param id 企业需求主键
     * @return 结果
     */
    @Override
    public int deleteCompanyNeedById(Long id) {
        return companyNeedMapper.deleteCompanyNeedById(id);
    }
}
